<?php
// @formatter:off
/**
 * @file c.stats.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.stats.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/themes/default/controlers/contents/c.stats.php $
 */
// @formatter:on

global $wcComicsHolder;
global $wcDatabaseAccess;
global $wcDefaults;
global $wctDefaults;

if($wcProfile->allowedTo(WC_PERM_STATS_VIEW)) {
	$wcThemeAssigns["PATH"] = "Statistics";
	$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.stats.html";

	$wcThemeAssigns["stats-max-time"] = $wcDefaults["stats-max-time"];

	$db = WCConnection::Instance();
	$dbprefix = $wcDatabaseAccess['prefix'];
	$gid = $wcGroup->id();

	//
	// Especial Tags Weight
	// @{
	$specialTags = array();
	$comics_tags = array();
	$tagInFull = array(
		WC_SPECIAL_TAG_UNTAGGED,
		WC_SPECIAL_TAG_FEW_TAGS
	);
	$fullCount = count($wcComicsHolder->itemIds($wcGroup));

	foreach($wcSpecialTags as $name) {
		$tag = $wcTagsHolder->itemByName($name, $wcGroup);
		if($tag && $tag->count > 0) {
			$specialTags[$tag->name] = "['{$tag->name}', {$tag->count}]";
			if(in_array($name, $tagInFull)) {
				$comics_tags[$tag->name] = "['{$name}', {$tag->count}]";
				$fullCount -= $tag->count;
			}
		} elseif(in_array($name, $tagInFull)) {
			$comics_tags[$tag->name] = "['{$name}', 0]";
		}
	}
	ksort($specialTags);

	$comics_tags["All"] = "['Rest', {$fullCount}]";
	ksort($comics_tags);
	$wcThemeAssigns["tags-specials"] = implode(",", $specialTags);
	$wcThemeAssigns["tags-specials-full"] = implode(",", $comics_tags);
	// @}

	//
	// Elapsed time in the last N days.
	// @{
	$elapsedLimit = $wctDefaults["stats-limits"]["elapsed"];
	$stmt = null;
	$stmtId = __CLASS__."::".__FUNCTION__;
	if($db->has($stmtId)) {
		$stmt = $db->get($stmtId);
	} else {
		$query = "select  st_t_caller,\n";
		$query .= "        st_t_action,\n";
		$query .= "        max(st_t_maximum) as st_t_maximum,\n";
		$query .= "        min(st_t_minimum) as st_t_minimum,\n";
		$query .= "        round(avg(st_t_average)) as st_t_average\n";
		$query .= "from    {$dbprefix}stats_timer\n";
		$query .= "where   st_t_group = '$gid'\n";
		$query .= "group by st_t_caller, st_t_action\n";
		$query .= "order by st_t_average asc, st_t_caller asc, st_t_action asc\n";
		$query .= "limit {$elapsedLimit}\n";

		$stmt = $db->prepare($stmtId, $query);
	}

	$elapsed = array();
	if($stmt->execute()) {
		foreach($stmt->fetchAll() as $row) {
			$elapsed[] = array(
				"name" => "{$row["st_t_caller"]}[{$row["st_t_action"]}]",
				"minimum" => $row["st_t_minimum"],
				"average" => $row["st_t_average"],
				"maximum" => $row["st_t_maximum"]
			);
		}
	}

	$names = array();
	$times = array();
	foreach($elapsed as $item) {
		$names[] = $item["name"];
		$times[] = $item["average"];
	}
	$wcThemeAssigns["elapsed-names"] = count($names) > 1 ? "'".implode("','", $names)."'" : "";
	$wcThemeAssigns["elapsed-times"] = implode(",", $times);
	$wcThemeAssigns["elapsed-height"] = $stmt->rowCount() * 24 + 50;
	$wcThemeAssigns["elapsed-limit"] = $elapsedLimit;
	// @}

	//
	// Most relevant pages
	// @{
	$relevantsLimit = $wctDefaults["stats-limits"]["elapsed"];
	$stmt = null;
	$stmtId = __CLASS__."::".__FUNCTION__."[F]";
	if($db->has($stmtId)) {
		$stmt = $db->get($stmtId);
	} else {
		$query = "select  st_t_caller,\n";
		$query .= "        st_t_action,\n";
		$query .= "        round((sum(st_t_times) * avg(st_t_average))/100) as st_t_factor\n";
		//$query .= "        round(avg(st_t_times*st_t_average))/100 as st_t_factor\n";
		$query .= "from    wc_stats_timer\n";
		$query .= "where   st_t_group = '$gid'\n";
		$query .= "group by st_t_caller, st_t_action\n";
		$query .= "order by st_t_factor asc\n";
		$query .= "limit {$relevantsLimit}\n";

		$stmt = $db->prepare($stmtId, $query);
	}

	$names = array();
	$factors = array();
	if($stmt->execute()) {
		foreach($stmt->fetchAll() as $row) {
			$names[] = "{$row["st_t_caller"]}[{$row["st_t_action"]}]";
			$factors[] = $row["st_t_factor"];
		}
	}
	$wcThemeAssigns["factors-names"] = count($names) > 1 ? "'".implode("','", $names)."'" : "";
	$wcThemeAssigns["factors-values"] = implode(",", $factors);
	$wcThemeAssigns["factors-height"] = $stmt->rowCount() * 24 + 50;
	$wcThemeAssigns["factors-limit"] = $relevantsLimit;
	// @}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to visit the statistics page";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>